package com.windoent.yugioh.ui.activity.setting;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.MainActivity;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.dialog.SucessFailAutoDismissDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.lang.ref.WeakReference;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/12 17:28
 * desc   :修改邮箱
 */
public class SettingChangeEmailActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private EditText mEtEmailNumber;
    private TextView mTvSend;
    private EditText mEtEmailCode;
    private Button mBtnGetCode;


    @Override
    public int getLayout() {
        return R.layout.activity_setting_change_email;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mEtEmailNumber = (EditText) findViewById(R.id.et_email_number);
        mTvSend = (TextView) findViewById(R.id.tv_send);
        mEtEmailCode = (EditText) findViewById(R.id.et_email_code);
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number = mEtEmailNumber.getText().toString().trim();
                getEmailCode(number);
            }
        });
        mBtnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number = mEtEmailNumber.getText().toString().trim();
                String code = mEtEmailCode.getText().toString().trim();
                if (BaseMethod.isNull(number)) {
                    ToastUtils.showText(getString(R.string.string_email_hint));
                    return;
                }
                if (BaseMethod.isNull(code)) {
                    ToastUtils.showText(getString(R.string.string_input_your_email_verify_code));
                    return;
                }
                //TODO 下一步
                bindEmail(number, code);
            }
        });

    }

    //绑定邮箱
    private void bindEmail(String number, String code) {
        showLoadingDialog("");
        PhoneLive.bindEmail(getUserToken(), code, number, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean json = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(json.getMsg());
                            bindEmailDialog(true);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    //发送邮箱
    private void getEmailCode(String number) {
        showLoadingDialog("");
        PhoneLive.getEmailCode(number, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean json = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(json.getMsg());
                            startTimer();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //绑定邮箱
    private void bindEmailDialog(boolean sucess) {
        SucessFailAutoDismissDialog dialog = new SucessFailAutoDismissDialog(mContext, new SucessFailAutoDismissDialog.OnInterface() {
            @Override
            public void onDissmiss() {
                //跳转到【账号安全】页面
                if (true)
                    startActivity(new Intent(mContext, MainActivity.class));
            }
        });
        dialog.setTvContent(true, R.drawable.drawable_dialog_sucess, getString(R.string.string_bind_sucess), "", "");
//        dialog.setTvContent(false, R.drawable.drawable_dialog_fail, getString(R.string.string_auth_real_name_fail), getString(R.string.string_check_input_more), "");
        dialog.show();

    }

    //开始倒计时
    private void startTimer() {
        mTvSend.setEnabled(false);
        SimpleUtils.startTimer(new WeakReference<TextView>(mTvSend), getString(R.string.string_send_phone_code_number), 60, 1, new SimpleUtils.TimerListener() {
            @Override
            public void onFinished() {
                mTvSend.setEnabled(true);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "设置修改邮箱界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "设置修改邮箱界面");
    }
}
